package com.jackykeke.kdatabasedemo

import java.io.*
import java.nio.charset.Charset
import java.util.*
import java.util.zip.ZipEntry
import java.util.zip.ZipFile

class ZipUtils {

    companion object{
        /**
         * 解压缩功能. 将ZIP_FILENAME文件解压到ZIP_DIR目录下. NOTE: 如果目录中含有中文名称， 要用substr = new
         * String(substr.getBytes("8859_1"), "GB2312");这样的语句转换，否则为乱码<br></br>
         *
         * @param zipFile
         * 要解压的件
         * @param folder
         * 解压到的位置
         * @return File success for unZipFile or null otherwise
         */
        fun unZip(zipFile: File?, folder: File): File? {
            var ok = true
            var unZipFile: File? = null
            try {
                val zfile = ZipFile(zipFile)
                val zList: Enumeration<*> = zfile.entries()
                var ze: ZipEntry? = null
                val buf = ByteArray(1024)
                while (zList.hasMoreElements()) {
                    ze = zList.nextElement() as ZipEntry
                    if (ze.isDirectory) {
                        var dirstr = (folder.path + File.separator
                                + ze!!.name)
                        dirstr = String(
                            dirstr.toByteArray(charset("8859_1")),
                            Charset.forName("GB2312")
                        )

                        val f = File(dirstr)
                        f.mkdirs()
                        if (null == unZipFile) {
                            // 第一层是解压后的文件啦
                            unZipFile = f
                        }
                        continue
                    }
                    val file: File = getFileName(folder.path, ze!!.name)
                    val os: OutputStream = BufferedOutputStream(
                        FileOutputStream(file)
                    )
                    val inputStream: InputStream = BufferedInputStream(
                        zfile.getInputStream(ze)
                    )
                    var readLen = 0
                    while (inputStream.read(buf, 0, 1024).also { readLen = it } != -1) {
                        os.write(buf, 0, readLen)
                    }
                    inputStream.close()
                    os.close()
                    if (null == unZipFile) {
                        // 第一层是解压后的文件啦
                        unZipFile = file
                    }
                }
                zfile.close()
            } catch (e: Exception) {
                ok = false
                e.printStackTrace()
            }
            if (!ok) {
                unZipFile = null
            }
            return unZipFile
        }


        /**
         * 给定根目录，返回一个完整路径的文件名.<br></br>
         * NOTE: 如果目录中含有中文名称， 要用substr = new String(substr.getBytes("8859_1"),
         * "GB2312");这样的语句转换，否则为乱码<br></br>
         *
         * @param baseDir
         * 指定根目录
         * @param zipEntryFileName
         * 相对路径名，来自于ZipEntry中的name
         * @return java.io.File 实际的文件
         */
        @Throws(java.lang.Exception::class)
        private fun getFileName(baseDir: String, zipEntryFileName: String): File {
            val dirs = zipEntryFileName.split("/").toTypedArray()
            var ret = File(baseDir)
            var substr: String? = null
            if (dirs.size > 1) {
                for (i in 0 until dirs.size - 1) {
                    substr = dirs[i]
                    substr = String(substr.toByteArray(charset("8859_1")),  Charset.forName("GB2312"))
                    ret = File(ret, substr)
                }
                if (!ret.exists()) {
                    ret.mkdirs()
                }
                substr = dirs[dirs.size - 1]
                substr = String(substr.toByteArray(charset("8859_1")), Charset.forName("GB2312"))
                ret = File(ret, substr)
                return ret
            } else {
                substr = String(zipEntryFileName.toByteArray(charset("8859_1")), Charset.forName("GB2312"))
                ret = File(ret, substr)
            }
            return ret
        }

    }
}



























